//
//  BCCoinTypeQrCodeView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/15.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCCoinTypeQrCodeView.h"

//生成二维码
#import "SGQRCode.h"

#import "UserWalletChargeResponse.h"

@interface BCCoinTypeQrCodeView ()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *titleLabel;

/**
 *addressLabel
 */
@property (nonatomic, strong) QMUILabel *addressLabel;

/**
 *qrImageView
 */
@property (nonatomic, strong) UIImageView *qrImageView;

/**
 *instructionsLabel
 */
@property (nonatomic, strong) QMUILabel *instructionsLabel;

@end

@implementation BCCoinTypeQrCodeView

/**
 title
 */
+ (UIFont *)titleFont {
    return [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
}
+ (UIFont *)addressFont {
    return [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
}

+ (CGSize)qrIVSize {
    return CGSizeMake(225.0, 225.0);
}

+ (CGFloat)titleHeight {
    return [self titleFont].lineHeight;
}
+ (CGFloat)addressHeight {
    return [self addressFont].lineHeight*2+4.0;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.alwaysBounceVertical = true;
        [self setupSubviews];
        //
        self.showsVerticalScrollIndicator = false;
    }
    return self;
}
- (void)setupSubviews {
    self.contentView = [UIView new];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.width.mas_equalTo(self);
        make.height.mas_equalTo(0);
    }];
    //
    self.titleLabel = [UILabel new];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [self.class titleFont];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#525252"];
    self.titleLabel.adjustsFontSizeToFitWidth = true;
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(12.0);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).offset(-40.0);
        make.height.mas_equalTo([self.class titleHeight]);
    }];
    //
    self.addressLabel = [QMUILabel new];
    self.addressLabel.canPerformCopyAction = true;
    self.addressLabel.menuItemTitleForCopyAction = BCLocalizedKey(@"general_copy");
    self.addressLabel.font = [self.class addressFont];
    self.addressLabel.textColor = [UIColor qmui_colorWithHexString:@"#696969"];
    self.addressLabel.textAlignment = NSTextAlignmentCenter;
    self.addressLabel.numberOfLines = 2;
    [self.contentView addSubview:self.addressLabel];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom);
        make.left.mas_equalTo(self.contentView).offset(20.0);
        make.width.mas_equalTo(self.contentView).offset(-40.0);
        make.height.mas_equalTo([self.class addressHeight]);
    }];
    //
    self.qrImageView = [UIImageView new];
    self.qrImageView.backgroundColor = [UIColor generalBackgroundColor];
    [self.contentView addSubview:self.qrImageView];
    [self.qrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.addressLabel.mas_bottom);
        make.centerX.mas_equalTo(self.contentView);
        make.size.mas_equalTo([self.class qrIVSize]);
    }];
    //
    self.instructionsLabel = [QMUILabel new];
    self.instructionsLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.instructionsLabel.numberOfLines = 0;
    self.instructionsLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.instructionsLabel];
    [self.instructionsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(20.0);
        make.top.mas_equalTo(self.qrImageView.mas_bottom).offset(19.0);
        make.width.mas_equalTo(self.contentView).offset(-40.0);
    }];
    
    
    //addressLabel 复制后的回调
    WEAK_SELF
    self.addressLabel.didCopyBlock = ^(QMUILabel *label, NSString *stringCopied) {
        if (wself.codeDelegate && [wself.codeDelegate respondsToSelector:@selector(pasteAddressAction:)]) {
            [wself.codeDelegate pasteAddressAction:stringCopied];
        }
    };
}

- (void)showContent:(BOOL)show {
    self.contentView.alpha = show?1:0;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}
- (void)setAddress:(NSString *)address {
    _address = address;
    self.addressLabel.text = address;
    //
    if (address.length) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            UIImage *qrImage = [SGQRCodeGenerateManager SG_generateWithDefaultQRCodeData:address imageViewWidth:[self.class qrIVSize].width];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.qrImageView.image = qrImage;
            });
        });
    }else {
        self.qrImageView.image = nil;
    }
}
- (void)setInstructions:(NSString *)instructions {
    _instructions = instructions;
    self.instructionsLabel.text = instructions;
    [self.instructionsLabel sizeToFit];
}

- (UIImage *)qrImage {
    return self.qrImageView.image;
}

- (void)setTitle:(NSString *)title adress:(NSString *)address qrImageURL:(NSString *)imageURL instructions:(NSString *)instructions {
    [self setContentOffset:CGPointZero animated:false];
    //
    self.title = title;
    self.address = address;
    self.instructions = instructions;
    //
    CGFloat contentHeight = CGRectGetMaxY(self.instructionsLabel.frame)+20.0;
    self.contentSize = CGSizeMake(0, contentHeight);
}

- (void)setChargeData:(UserWalletChargeData *)chargeData {
    _chargeData = chargeData;
    //
    [self setContentOffset:CGPointZero animated:false];
    self.title = chargeData.flow.addr;
    if (chargeData.address.length) {
        self.address = chargeData.address;
    }else {
        self.address = nil;
    }
    //
    NSString *instructions = chargeData.flow.desc_html;
//    NSString * instructions = @"<html><body> Some html string \n <font color=\"red\">This is some text!</font> </body></html>";
    NSAttributedString * instructionsAttri = [[NSAttributedString alloc] initWithData:[instructions dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.instructionsLabel.attributedText = instructionsAttri;
    [self.instructionsLabel sizeToFit];
    //
    [self layoutIfNeeded];
    CGFloat contentHeight = CGRectGetMaxY(self.instructionsLabel.frame)+20.0;
    self.contentSize = CGSizeMake(0, contentHeight);
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contentHeight);
    }];
}


@end
